import ArrayQueue from "./01_实现队列结构";

/**
 *
 * @param names 参与游戏的人
 * @param num 每次传花的人数
 * @returns 返回最后剩下的人的索引
 */
function hoxPotato<T extends string>(names: T[], num: number) {
  if (names.length === 0) return -1;

  const queue = new ArrayQueue<T>();

  for (const item of names) {
    queue.enqueue(item);
  }

  while (queue.size() > 1) {
    for (let i = 1; i < num; i++) {
      const name = queue.dequeue();
      if (name) queue.enqueue(name);
    }
    queue.dequeue();
  }

  const leftName = queue.dequeue()!;
  const index = names.indexOf(leftName);
  return index;
}

const index = hoxPotato<string>(
  ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ttt", "uuu"],
  4
);
console.log(index); // 5
